<template>
  <div class="dashboard-container">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>个人信息</span>
      </div>
      <div class="text item">
        <div class="InfoList">
           <span class="textItem">用户名</span> : <span class="textValue">{{ !!updateInfo.name ? updateInfo.name : '-' }}</span>
        </div>
        <div class="InfoList">
          <span class="textItem">邮箱</span> : <span class="textValue">{{ !!updateInfo.email ? updateInfo.email : '-'  }}</span>
        </div>
        <div class="InfoList">
          <span class="textItem">电话</span> : <span class="textValue">{{ !!updateInfo.phone ? updateInfo.phone : '-'}}</span>
        </div>
        <div class="InfoList">
          <span class="textItem">角色</span> : <span class="textValue">{{ updateInfo.roleId | roleMap(updateInfo.roleId)}}</span>
        </div>
        <div class="InfoList" v-if="updateInfo.roleId == 1">
          <span class="textItem">临时公司ID</span>
          :
          <span class="textValue">
            <el-select v-model="companyId"  @change="companyChange" clearable filterable placeholder="请选择">
              <el-option
                v-for="item in companyArr"
                :key="item.companyId"
                :label="item.companyName"
                :value="item.companyId">
              </el-option>
            </el-select>
          </span>
        </div>
      </div>
      <div class="button-group" v-if="updateInfo.roleId < 3">
        <el-button style="margin-right:35px;" type="primary" size="mini" @click="infoFormVisible = true">修改资料</el-button>
        <el-button type="primary" size="mini" @click="pwdFormVisible = true">修改密码</el-button>
      </div>
    </el-card>

    <el-dialog @close="cancledialog('userForm')"  width="400px" title="修改资料" :visible.sync="infoFormVisible">
      <el-form :model="updateInfo" :rules="userRules" ref="userForm">
        <el-form-item label="用户名" prop="name" :label-width="formLabelWidth">
          <el-input v-model.sync.trim="updateInfo.name"  auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email" :label-width="formLabelWidth">
          <el-input v-model.sync.trim="updateInfo.email"  auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="电话" prop="phone" :label-width="formLabelWidth">
          <el-input  v-model.trim="updateInfo.phone" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="infoFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="update('userForm')">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog @close="cancledialog('pwdForm')"  width="400px" title="修改密码" :visible.sync="pwdFormVisible">
      <el-form :model="pwdData" :rules="pwdRules" ref="pwdForm">
        <el-form-item label="原始密码" prop="oldPwd" :label-width="formLabelWidth">
          <el-input v-model.sync="pwdData.oldPwd"  auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="新密码" prop="newPwd" :label-width="formLabelWidth">
          <el-input v-model.sync="pwdData.newPwd"  auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="checkNewPwd" :label-width="formLabelWidth">
          <el-input v-model.syncsync="pwdData.checkNewPwd"  auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="pwdFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="upDataPwd('pwdForm')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { updateInfo, updatePwd } from '@/api/home'
import { Message } from 'element-ui'
import {getCompany} from '@/api/dataStock'
import { getRanNum , base64encode } from '@/utils/index';

export default {
  name: 'Home',
  created(){
    this.pwdData.userId = this.updateInfo.userId;
  },
  data() {
    const checkNewPwd = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.pwdData.newPwd) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    }
    const CheckTel = (rule, value, callback) => {
      let tempValue = value * 1
      if (!value) {
        callback(new Error('请输入电话号码'));
      } else if (!Number.isInteger(tempValue)) {
        callback(new Error('电话号码必须是数字'));
      } else if (value.toString().length != 11) {
        callback(new Error('电话号码必须是11位'));
      } else {
        callback();
      }
    }
    return {
      companyId:'',
      companyArr:[],
      pwdData:{
        oldPwd:'',
        newPwd:'',
        checkNewPwd:'',
        parentId:''
      },
      infoFormVisible: false,
      pwdFormVisible: false,
      formLabelWidth: '120px',
      pwdRules:{
        oldPwd:[
          {required: true, message: '请输入原始密码', trigger: 'blur'},
          {max: 16, message: '原始密码长度不能大于16位', trigger: 'blur'}
        ],
        newPwd:[
          {required: true, message: '请输入密码', trigger: 'blur'},
          {min: 6, message: '密码长度不能小于6位', trigger: 'blur'},
          {max: 16, message: '密码长度不能大于16位', trigger: 'blur'}
        ],
        checkNewPwd: [
          { validator: checkNewPwd, trigger: 'blur' }
        ]
      },
      userRules: {
        name: [
          {required: true, message: '请输入用户名', trigger: 'blur'}
        ],
        email: [
          {required: true, message: '请输入邮箱', trigger: 'blur'},
        ],
        phone: [
          { validator: CheckTel, trigger: 'blur' }
        ]
      }
    }
  },
  activated(){
    //如果事高级客服直接跳工作台
    this.updateInfo.roleId == 7 ? this.$router.replace({ path: '/seni/index' }) : ''
  },
  computed: {
    ...mapGetters([
      'updateInfo'
    ])
  },
  mounted(){
    this.getCompany();
  },
  methods: {
    companyChange(){
      this.$store.dispatch('SetCompanyId',this.companyId);
    },
    getCompany(){
      getCompany().then(res => {
        let temp = res.data.info;
        this.companyArr = temp;
      }).catch(res => {
        this.$message.error(res)
      })
    },
    cancledialog(userForm){
      //重置表单
      this.$refs[userForm].resetFields();
    },
    upDataPwd(formName){
      this.$refs[formName].validate((valid) => {
        if (valid) {
          var temp = Object.assign({},this.pwdData);
          temp.oldPwd = getRanNum(8) + base64encode(temp.oldPwd) + getRanNum(8);
          temp.newPwd = getRanNum(8) + base64encode(temp.newPwd) + getRanNum(8);
          updatePwd(temp).then(()=>{
            this.pwdFormVisible = false;
            this.$message.success('修改成功')
          }).catch(res=>{
            this.$message.error('修改失败,请确认您的原始密码')
          })
        }else {
          console.log('error submit!!');
          return false;
        }
      })
    },
    update(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$store.dispatch('UpdateInfo', this.updateInfo).then(()=>{
            this.infoFormVisible = false;
            window.location.reload();
          }).catch(res=>{
            console.log("error in this")
            Message.error(res)
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      })
    }
  }
}
</script>

<style type="text/scss" lang="scss" scoped>
  .InfoList{
    height: 30px;
    line-height: 30px;
    width: 100%;
    margin-left: 20px;
    margin-bottom: 10px;
    .textItem {
      display: inline-block;
      width: 100px;
      text-align: right;
    }
    .textValue {
      margin-left: 50px;
    }
  }
  .button-group {
    padding-left: 55px;
    margin-top: 30px;
  }
  .box-card {
    margin: 0 auto;
    width: 50%;
    min-width: 435px;
  }

</style>
